package com.googlecode.classgrep.visitor.annotated;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.commons.EmptyVisitor;

import com.googlecode.classgrep.GrepRobotInternal;
import com.googlecode.classgrep.grep.annotated.FieldGrep;
import com.googlecode.classgrep.info.AnnotationInfo;
import com.googlecode.classgrep.info.FieldInfo;

public class FieldDefVisitor extends CommonDef implements FieldVisitor {

	private FieldInfo info;
	
	private FieldGrep fieldGrep;
	
	public FieldDefVisitor(GrepRobotInternal grepClient, FieldInfo info) {
		super(grepClient);
		this.info = info;
		this.fieldGrep = grepClient.getFieldGrep();
	}

	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		AnnotationInfo annotationInfo = new AnnotationInfo(desc);
		if(grepClient.getAnnotationCriterion().contains(annotationInfo.getName())){
	        info.getAnnotations().add(annotationInfo);
	        List<FieldInfo> infos = fieldGrep.getAnnotatedFieldMap().get(annotationInfo.getName());
	        if (infos == null) {
	            infos = new ArrayList<FieldInfo>();
	            fieldGrep.getAnnotatedFieldMap().put(annotationInfo.getName(), infos);
	        }
	        infos.add(info);
	        return new AnnotationDefVisitor(grepClient, annotationInfo);
		}else{
			return new EmptyVisitor();
		}
	}

	public void visitAttribute(Attribute attr) {

	}

	public void visitEnd() {

	}

}
